//package com.ruoyi.core.config;
//
//import com.ruoyi.core.domain.ClientProductUser;
//import com.ruoyi.core.mapper.ClientProductUserMapper;
//import com.ruoyi.core.task.JsPluginRefreshJob;
//import com.ruoyi.core.task.JungleScoutCookieRefreshJob;
//import com.ruoyi.core.task.MerchantWordCookieRefreshOnOffUpdateJob;
//import com.ruoyi.system.domain.SysConfig;
//import com.ruoyi.system.service.ISysConfigService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.Trigger;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.SchedulingConfigurer;
//import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
//import org.springframework.scheduling.config.ScheduledTaskRegistrar;
//import org.springframework.scheduling.support.PeriodicTrigger;
//import org.springframework.scheduling.support.SimpleTriggerContext;
//import org.springframework.stereotype.Component;
//import org.springframework.util.Assert;
//import org.springframework.util.StringUtils;
//import reactor.core.scheduler.Scheduler;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.Timer;
//import java.util.TimerTask;
//import java.util.concurrent.TimeUnit;
//
///**
// * @Description:
// * @Company: 北京卡拉卡尔股份技术有限公司
// * @Author: Darren Ho
// * @Date: 10/20/2020 16:19
// */
//@EnableScheduling
//@Component
//public class SchedulingTaskConfig implements SchedulingConfigurer, InitializingBean {
//
//    private final Logger logger = LoggerFactory.getLogger(SchedulingTaskConfig.class);
//
//
//    @Autowired
//    private ISysConfigService sysConfigService;
//
//    @Autowired
//    private MerchantWordCookieRefreshOnOffUpdateJob merchantWordCookieRefreshOnOffUpdateJob;
//
//    @Autowired
//    private JungleScoutCookieRefreshJob junglescoutCookieRefreshJob;
//
//    @Autowired
//    private JsPluginRefreshJob jsPluginRefreshJob;
//
//    @Autowired
//    private ClientProductUserMapper clientProductUserMapper;
//
//    // 0/10 * * * * ?   测试使用  0 0/30 * * * ?
////    private String cron;
//
//    private Integer cookieRefreshTimeInterval;
//
//    private Integer jungleScoutCookieRefreshTimeInterval;
//
//
//    private Integer jsPluginCookieRefreshTimeInterval;
//
//    ConcurrentTaskScheduler concurrentTaskScheduler;
//
//
//    @Override
//    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//        //  亚马逊网站
//        taskRegistrar.addTriggerTask(() -> {
//            // 业务逻辑
//
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            logger.info("cookie refresh on-off update time {} ", simpleDateFormat.format(new Date()));
//            String merchantWordProductId = sysConfigService.selectConfigByKey("MERCHANT_WORD_PRODUCT_ID");
//
//            if (!StringUtils.hasText(merchantWordProductId)) {
//                return;
//            }
//
////            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////            logger.info("cookie refresh on-off update time {} ", simpleDateFormat.format(new Date()));
//
//
//            Long productId = Long.parseLong(merchantWordProductId);
//            List<ClientProductUser> clientProductUsers = clientProductUserMapper.findByProductId(productId);
//            merchantWordCookieRefreshOnOffUpdateJob.start(clientProductUsers);
//
//            // 业务逻辑
//        }, getTrigger());
//
//
//        //  jungle  scout
//        taskRegistrar.addTriggerTask(() -> {
//
//            // 业务逻辑
//
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            logger.info("jungle scout cookie refresh  update time {} ", simpleDateFormat.format(new Date()));
//            String jungleScoutProductId = sysConfigService.selectConfigByKey("JUNGLE_SCOUT_PRODUCT_ID");
//
//            if (!StringUtils.hasText(jungleScoutProductId)) {
//                return;
//            }
//
//            Long productId = Long.parseLong(jungleScoutProductId);
//            List<ClientProductUser> clientProductUsers = clientProductUserMapper.findByProductId(productId);
//            junglescoutCookieRefreshJob.start(clientProductUsers);
//        }, getJungleScoutTrigger());
//
//        // js  plugin
//
//    }
//
//    private Trigger getJungleScoutTrigger() {
//
//        return triggerContext -> {
//            // 触发器
////            Assert.hasText(jungleScoutCron, "jungle scout  corn must has text");
////            CronTrigger trigger = new CronTrigger(jungleScoutCron);
////            return trigger.nextExecutionTime(triggerContext);
//            if (jungleScoutCookieRefreshTimeInterval == null) {
//                logger.error("jungle scout cookie refresh time interval must not be null ");
//            }
//            PeriodicTrigger trigger = new PeriodicTrigger(jungleScoutCookieRefreshTimeInterval, TimeUnit.MINUTES);
//            trigger.setFixedRate(true);
//            return trigger.nextExecutionTime(triggerContext);
//        };
//
////        PeriodicTrigger trigger = new PeriodicTrigger(jungleScoutCookieRefreshTimeInterval, TimeUnit.MINUTES);
////        trigger.setFixedRate(true);
//////        trigger.nextExecutionTime(simpleTriggerContext);
////        return trigger;
//
//    }
//
//    private Trigger getTrigger() {
//
//        return triggerContext -> {
//            // 触发器
////            Assert.hasText(cron, "corn must has text");
//
////            PeriodicTrigger trigger = new PeriodicTrigger(2, TimeUnit.MINUTES);
////            CronTrigger trigger = new CronTrigger(cron);
////            return trigger.nextExecutionTime(triggerContext);
//
//            if (cookieRefreshTimeInterval == null) {
//                logger.error("cookie refresh time interval must not be null ");
//            }
//            PeriodicTrigger trigger = new PeriodicTrigger(cookieRefreshTimeInterval, TimeUnit.MINUTES);
//            trigger.setFixedRate(true);
//            return trigger.nextExecutionTime(triggerContext);
//        };
//
////        PeriodicTrigger trigger = new PeriodicTrigger(cookieRefreshTimeInterval, TimeUnit.MINUTES);
////        trigger.setFixedRate(true);
////
//////        trigger.nextExecutionTime(simpleTriggerContext);
////        return trigger;
//    }
//
////    public void setCron(String cron) {
////        this.cron = cron;
////        logger.info("更新cron:[{}]", cron);
////    }
////
////    public void setJungleScoutCron(String jungleScoutCron) {
////        this.jungleScoutCron = jungleScoutCron;
////
////        logger.info("更新 Jungle scout Cron :[{}]", jungleScoutCron);
////    }
//
//
//    public void setCookieRefreshTimeInterval(Integer cookieRefreshTimeInterval) {
//        this.cookieRefreshTimeInterval = cookieRefreshTimeInterval;
//
//        logger.info("更新cookie refresh time interval :[{}]", cookieRefreshTimeInterval);
//    }
//
//    public void setJungleScoutCookieRefreshTimeInterval(Integer jungleScoutCookieRefreshTimeInterval) {
//        this.jungleScoutCookieRefreshTimeInterval = jungleScoutCookieRefreshTimeInterval;
//
//        logger.info("更新jungle scout cookie refresh time interval :[{}]", jungleScoutCookieRefreshTimeInterval);
//    }
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
////        String cookieRefreshCorn = sysConfigService.selectConfigByKey("COOKIE_REFRESH_CORN");
////
////        if (!StringUtils.hasText(cookieRefreshCorn)) {
////            SysConfig sysConfig = new SysConfig();
////            sysConfig.setConfigKey("COOKIE_REFRESH_CORN");
////            sysConfig.setConfigValue("0 0/30 * * * ?");
////            sysConfig.setConfigName("cookie刷新开关定时更新corn配置项");
////            sysConfig.setCreateBy("admin");
////            sysConfig.setConfigType("Y");
////            sysConfigService.insertConfig(sysConfig);
////        }
////
////
////        String JunglescoutCron = sysConfigService.selectConfigByKey("JUNGLE_SCOUT_COOKIE_REFRESH_CORN");
////
////        if (!StringUtils.hasText(JunglescoutCron)) {
////            SysConfig sysConfig = new SysConfig();
////            sysConfig.setConfigKey("JUNGLE_SCOUT_COOKIE_REFRESH_CORN");
////            sysConfig.setConfigValue("0 0/5 * * * ?");
////            sysConfig.setConfigName("JUNGLE_SCOUT_COOKIE 刷新开关定时更新corn配置项");
////            sysConfig.setCreateBy("admin");
////            sysConfig.setConfigType("Y");
////            sysConfigService.insertConfig(sysConfig);
////        }
//
//        int cookieRefreshTimeIntervalInter = 30;
//
//
//        String cookieRefreshTimeInterval = sysConfigService.selectConfigByKey("COOKIE_REFRESH_TIME_INTERVAL");
//
//        if (!StringUtils.hasText(cookieRefreshTimeInterval)) {
//            SysConfig sysConfig = new SysConfig();
//            sysConfig.setConfigKey("COOKIE_REFRESH_TIME_INTERVAL");
//            sysConfig.setConfigValue(String.valueOf(cookieRefreshTimeIntervalInter));
//            sysConfig.setConfigName("cookie刷新开关定时更新时间配置项");
//            sysConfig.setCreateBy("admin");
//            sysConfig.setConfigType("Y");
//            sysConfigService.insertConfig(sysConfig);
//        } else {
//            cookieRefreshTimeIntervalInter = Integer.parseInt(cookieRefreshTimeInterval);
//        }
//        int jungleScoutCookieRefreshTimeIntervalInter = 60;
//
//        String jungleScoutCookieRefreshTimeInterval = sysConfigService.selectConfigByKey("JUNGLE_SCOUT_COOKIE_REFRESH_TIME_INTERVAL");
//
//        if (!StringUtils.hasText(jungleScoutCookieRefreshTimeInterval)) {
//            SysConfig sysConfig = new SysConfig();
//            sysConfig.setConfigKey("JUNGLE_SCOUT_COOKIE_REFRESH_TIME_INTERVAL");
//            sysConfig.setConfigValue(String.valueOf(jungleScoutCookieRefreshTimeIntervalInter));
//            sysConfig.setConfigName("JUNGLE_SCOUT_COOKIE 刷新开关定时更新时间配置项");
//            sysConfig.setCreateBy("admin");
//            sysConfig.setConfigType("Y");
//            sysConfigService.insertConfig(sysConfig);
//        } else {
//            jungleScoutCookieRefreshTimeIntervalInter = Integer.parseInt(jungleScoutCookieRefreshTimeInterval);
//        }
//
//
//        setCookieRefreshTimeInterval(cookieRefreshTimeIntervalInter);
//        setJungleScoutCookieRefreshTimeInterval(jungleScoutCookieRefreshTimeIntervalInter);
//
//        new Timer().scheduleAtFixedRate(new CustomTimerTask(this), 1000, 10 * 1000);
//    }
//
//    private class CustomTimerTask extends TimerTask {
//
//        public CustomTimerTask(SchedulingTaskConfig schedulingTaskConfig) {
//            this.schedulingTaskConfig = schedulingTaskConfig;
//        }
//
//        private SchedulingTaskConfig schedulingTaskConfig;
//
//        public void run() {
////            String cookieRefreshCorn = sysConfigService.selectConfigByKey("COOKIE_REFRESH_CORN");
////            Assert.hasText(cookieRefreshCorn, "cookie refresh corn must has text");
////            if (!cookieRefreshCorn.equals(schedulingTaskConfig.cron)) {
////                schedulingTaskConfig.setCron(cookieRefreshCorn);
////            }
////
////
////            String jungleScoutCron = sysConfigService.selectConfigByKey("JUNGLE_SCOUT_COOKIE_REFRESH_CORN");
////            Assert.hasText(jungleScoutCron, "jungle scout cookie refresh corn must has text");
////            if (!jungleScoutCron.equals(schedulingTaskConfig.jungleScoutCron)) {
////                schedulingTaskConfig.setJungleScoutCron(jungleScoutCron);
////            }
//
//
//            String cookieRefreshTimeInterval = sysConfigService.selectConfigByKey("COOKIE_REFRESH_TIME_INTERVAL");
//            Assert.hasText(cookieRefreshTimeInterval, "cookie refresh time interval must has text");
//            Integer cookieRefreshTimeIntervalInter = Integer.parseInt(cookieRefreshTimeInterval);
//            if (cookieRefreshTimeIntervalInter != schedulingTaskConfig.cookieRefreshTimeInterval) {
//                schedulingTaskConfig.setCookieRefreshTimeInterval(cookieRefreshTimeIntervalInter);
//            }
//
//
//            String jungleScoutCookieRefreshTimeInterval = sysConfigService.selectConfigByKey("JUNGLE_SCOUT_COOKIE_REFRESH_TIME_INTERVAL");
//            Assert.hasText(jungleScoutCookieRefreshTimeInterval, "jungle scout cookie refresh corn must has text");
//
//            Integer cookieRefreshTimeIntervalInterInter = Integer.parseInt(jungleScoutCookieRefreshTimeInterval);
//            if (cookieRefreshTimeIntervalInterInter != schedulingTaskConfig.jungleScoutCookieRefreshTimeInterval) {
//                schedulingTaskConfig.setJungleScoutCookieRefreshTimeInterval(cookieRefreshTimeIntervalInterInter);
//            }
//        }
//
//    }
//
//
//}
